<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input
          v-model="query.value"
          clearable
          placeholder="输入搜索内容"
          style="width: 200px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-select v-model="query.type" clearable placeholder="类型" class="filter-item" style="width: 130px">
          <el-option v-for="item in queryTypeOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="700px"
      >
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px">
          <!--<el-form-item label="id">-->
          <!--<el-input v-model="form.id" style="width: 370px;" />-->
          <!--</el-form-item>-->
          <!--<el-form-item label="cid" prop="cid">-->
          <!--<el-input v-model="form.cid" style="width: 370px;" />-->
          <!--</el-form-item>-->
          <el-form-item label="指挥级别" prop="commandLevelCode">
            <el-select v-model="form.commandLevelCode" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.command_level"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="应急指挥名称" prop="name">
            <el-input v-model="form.name" style="width: 370px;" />
          </el-form-item>

          <!--<el-form-item label="用户" prop="userId">-->
          <!--<el-input v-model="form.userId" style="width: 370px;" />-->
          <!--</el-form-item>-->

          <!--<el-form-item label="用户" prop="userId">-->
          <!--<treeselect-->
          <!--v-model="form.userId"-->
          <!--:options="users"-->
          <!--:load-options="loadUsers"-->
          <!--style="width: 178px"-->
          <!--placeholder="选择人员"-->
          <!--/>-->
          <!--</el-form-item>-->

          <el-form-item label="用户" prop="userId">
            <el-select
              v-model="form.userId"
              style="width: 260px"
              placeholder="请选择负责人"
            >
              <el-option
                v-for="item in depManagers"
                :key="item.id"
                :label="item.username"
                :value="item.id"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="职责">
            <el-input v-model="form.duty" :rows="3" type="textarea" style="width: 370px;" />
          </el-form-item>

          <!--<el-form-item label="创建时间" prop="createTime">-->
          <!--<el-input v-model="form.createTime" style="width: 370px;" />-->
          <!--</el-form-item>-->
          <!--<el-form-item label="更新时间" prop="updateTime">-->
          <!--<el-input v-model="form.updateTime" style="width: 370px;" />-->
          <!--</el-form-item>-->
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <!--<el-table-column prop="id" label="id" />-->
        <!--<el-table-column prop="cid" label="cid" />-->
        <el-table-column prop="name" label="应急指挥名称" />
        <el-table-column prop="duty" label="职责" />
        <el-table-column prop="commandLevelCode" label="指挥级别">
          <template slot-scope="scope">
            {{ dict.label.command_level[scope.row.commandLevelCode] }}
          </template>
        </el-table-column>
        <el-table-column prop="username" label="名称" />
        <el-table-column prop="phone" label="联系方式" />
        <!--<el-table-column prop="createTime" label="创建时间">-->
        <!--<template slot-scope="scope">-->
        <!--<span>{{ parseTime(scope.row.createTime) }}</span>-->
        <!--</template>-->
        <!--</el-table-column>-->
        <!--<el-table-column prop="updateTime" label="更新时间">-->
        <!--<template slot-scope="scope">-->
        <!--<span>{{ parseTime(scope.row.updateTime) }}</span>-->
        <!--</template>-->
        <!--</el-table-column>-->
        <el-table-column v-permission="['admin','command:edit','command:del']" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import { queryUserDepartment, getDeptSuperior } from '@/api/system/dept'
import crudCommand from '@/api/emergency/command'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import Treeselect from '@riophae/vue-treeselect'
import { mapGetters } from 'vuex'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { LOAD_CHILDREN_OPTIONS } from '@riophae/vue-treeselect'
import { getAllUsers } from '@/api/system/user'

const defaultForm = {
  id: null,
  cid: null,
  name: null,
  duty: null,
  commandLevelCode: null,
  userId: null,
  createTime: null,
  updateTime: null
}
export default {
  name: 'Command',
  components: { Treeselect, pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['command_level'],
  cruds() {
    return CRUD({ title: '应急指挥', url: 'api/command', sort: 'id,desc', crudMethod: { ...crudCommand }})
  },
  data() {
    return {
      permission: {
        add: ['admin', 'command:add'],
        edit: ['admin', 'command:edit'],
        del: ['admin', 'command:del']
      },
      depManagers: [],
      users: [],
      rules: {
        name: [
          { required: true, message: '应急指挥名称不能为空', trigger: 'blur' }
        ],
        commandLevelCode: [
          { required: true, message: '指挥级别不能为空', trigger: 'blur' }
        ],
        userId: [
          { required: true, message: '用户不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'id', display_name: 'id' },
        { key: 'name', display_name: '应急指挥名称' },
        { key: 'commandLevelCode', display_name: '指挥级别id' },
        { key: 'userId', display_name: '用户id' }
      ]
    }
  },
  methods: {
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      this.getDepManagers()
      this.loadUsers()
      const query = this.query
      if (query.type && query.value) {
        this.crud.params[query.type] = query.value
      }
      return true
    },
    // 获取弹窗内部门数据
    loadUsers() {
      queryUserDepartment().then(res => {
        console.log('queryUserDepartment')
        console.log(res)
        parentNode.children = res.content.map(function(obj) {
          if (obj.hasChildren) {
            obj.children = null
          }
          return obj
        })
      })
    },
    getDepManagers() {
      getAllUsers().then(res => {
        console.log(res.content)
        this.depManagers = res.content
      }).catch(() => { })
    }
  }
}
</script>

<style scoped>

</style>
